package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.DepartmentLeftApadter;
import com.zmx.patientend.adapter.DepartmentRightApadter;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.Department;
import com.zmx.patientend.pojo.DepartmentCategory;
import com.zmx.patientend.pojo.HospMessage;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 时间：2020/4/30 21:37
 * 功能：根据科室选择挂号
 */
public class MakeAppointmentNextActivity extends BaseActivity {


    @BindView(R.id.rv_rvliandong_Left)
    RecyclerView mRvLeft;
    @BindView(R.id.rv_rvliandong_Right)
    RecyclerView mRvRight;

    private DepartmentLeftApadter leftApadter;
    private DepartmentRightApadter rightApadter;

    private List<DepartmentCategory> dcs;
    private List<Department> ds;

    private String hosp_id;
    private String f_name;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_make_appointment_next;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);

        hosp_id = getIntent().getStringExtra("hosp_id");

        dcs = new ArrayList<>();
        mRvLeft.setLayoutManager(new LinearLayoutManager(this));
        leftApadter = new DepartmentLeftApadter(this);
        mRvLeft.setAdapter(leftApadter);

        ds = new ArrayList<>();
        mRvRight.setLayoutManager(new LinearLayoutManager(this));
        rightApadter = new DepartmentRightApadter(this);
        mRvRight.setAdapter(rightApadter);

        rightApadter.setListAndNotifyDataSetChanged(ds);//就是设置list然后刷新
        leftApadter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                Log.e("设置右边数据","设置右边数据");
                leftApadter.mCheckedPosition = listPosition;
                leftApadter.notifyDataSetChanged();
                ds.clear();
                f_name = dcs.get(listPosition).getName();
                getHospClass_a(dcs.get(listPosition).getId()+"");

            }
        });

        getHospClass_a();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:

                    try {

                        JSONArray array = new JSONArray(msg.obj.toString());
                        Gson gson = new Gson();
                        for (int i=0;i<array.length();i++){

                            DepartmentCategory dc = gson.fromJson(array.getJSONObject(i).toString(),DepartmentCategory.class);
                            dcs.add(dc);

                        }

                        leftApadter.setListAndNotifyDataSetChanged(dcs);//就是设置list然后刷新
                        getHospClass_a(dcs.get(0).getId()+"");

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

                case 2:
                    Log.e("数据","数据"+msg.obj.toString());
                    try {

                        JSONArray array = new JSONArray(msg.obj.toString());
                        Gson gson = new Gson();

                        Department d = new Department();
                        List<DepartmentCategory> lists = new ArrayList<>();
                        for (int i=0;i<array.length();i++){

                            DepartmentCategory dc = gson.fromJson(array.getJSONObject(i).toString(),DepartmentCategory.class);
                            lists.add(dc);

                        }
                        d.setLists(lists);
                        d.setF_name(f_name);
                        ds.add(d);
                        rightApadter.setListAndNotifyDataSetChanged(ds);//就是设置list然后刷新

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

        }
    };

    /**
     * 获取大类目科室
     */
    public void getHospClass_a() {

        List<String> l = new ArrayList<>();
        l.add("0");
        l.add(hosp_id);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getHospClass");
        params.put("data[hosp_id]", hosp_id);
        params.put("data[fid]", 0);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/app/", params, handler, 1, 404);

    }


    public void getHospClass_a(String fid) {

        Log.e("fid","打发十分"+fid);
        List<String> l = new ArrayList<>();
        l.add(fid);
        l.add(hosp_id);
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(l)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getHospClass");
        params.put("data[hosp_id]", hosp_id);
        params.put("data[fid]", fid);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/app/", params, handler, 2, 404);

    }


}
